import java.util.concurrent.TimeUnit;

/**
 * @projectName: java-daily-questions
 * @package: PACKAGE_NAME
 * @className: DeadLock
 * @author: ZM
 * @description: 死锁
 * @date: 2021/8/23 16:38
 * @version: 1.0
 */
public class DeadLock {

    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();

        Thread t1 = new Thread(()-> {
            synchronized (a) {
                System.out.println(Thread.currentThread().getName() + "获得a锁");
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b){
                    System.out.println(Thread.currentThread().getName() + "获得b锁");
                }
            }
        }, "t1");
        Thread t2 = new Thread(()-> {
            synchronized (b) {
                System.out.println(Thread.currentThread().getName() + "获得b锁");
                try {
                    TimeUnit.MILLISECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println(Thread.currentThread().getName() + "获得a锁");
                }
            }
        }, "t2");

        t1.start();
        t2.start();
    }
}
